<?php

/*

Competition (competition.php)

*/

$page_title = "Competitions";
$rank_check = 1;
include "header.inc.php";

if ($end == "true")
{
	$numTickets = fetch("SELECT count(id) AS id FROM competition");
	$numBrian   = mysql_query("SELECT userid FROM competition WHERE pony = 'brian'");
	$numSteve   = mysql_query("SELECT userid FROM competition WHERE pony = 'steve'");
	$numSteve2  = fetch("SELECT count(id) AS id FROM competition WHERE pony = 'steve'");
	$numBrian2  = fetch("SELECT count(id) AS id FROM competition WHERE pony = 'brian'");

	if ($winner == "brian")
	{
		$prize = round(($numTickets[id] * 100) / $numBrian2[id]);
		while($getWinners = mysql_fetch_array($numBrian))
		{
			$getWinner = fetch("SELECT username FROM members2 WHERE id = '$getWinners[userid]'");
			points($game,$getWinner[username],"$prize");
		}
	}
	if ($winner == "steve")
	{
		$prize = round(($numTickets[id] * 100) / $numSteve2[id]);
		while($getWinners = mysql_fetch_array($numSteve))
		{
			$getWinner = fetch("SELECT username FROM members2 WHERE id = '$getWinners[userid]'");
			points($game,$getWinner[username],"$prize");
		}
	}
	mysql_query("DELETE FROM competition");
}

die ("The game is over. Brian won.");

if ($pony)
{
	if ($points >= 100)
	{
		points($game,$username,"-100");
	}
	if ($points < 100)
	{
		die("You do not have enough points to enter.");
	}
	if (($pony == "brian") OR ($pony == "steve"))
	{
		mysql_query("INSERT INTO competition VALUES ('', '$userid', '$pony', '$game')");
	}
}

$numTickets = fetch("SELECT count(id) AS id FROM competition");
$numBrian   = fetch("SELECT count(id) AS id FROM competition WHERE pony = 'brian'");
$numSteve   = fetch("SELECT count(id) AS id FROM competition WHERE pony = 'steve'");
$numBrian2  = fetch("SELECT count(id) AS id FROM competition WHERE pony = 'brian' AND userid = '$userid'");
$numSteve2  = fetch("SELECT count(id) AS id FROM competition WHERE pony = 'steve' AND userid = '$userid'");

print "$openHTML";

print "<p>Alright, brian and I are about to go play basketball, and we thought it'd be funny to let you people bet on the results. So, if you think you know who will win go ahead and place your bet of 100 points, the jackpot will be split between everyone who guessed properly ;)</p>

<p align=center><font size=+2><a href=competition.php?game=$game&pony=brian>Brian</a> -- <a href=competition.php?game=$game&pony=steve>Steve</a></font></p>

<p align=center><font size=+1>Tickets: $numTickets[id]<br>Bets for Brian: $numBrian[id]<br>Bets for Steve: $numSteve[id]</font></p>

<p align=center><font size=+1>Your Bets for Brian: $numBrian2[id]<br>Your Bets for Steve: $numSteve2[id]</font></p>";

print "$closeHTML";
?>